package io.renren.modules.generator.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.common.utils.excel.ExcelUtils;
import io.renren.modules.generator.entity.DtAreaEntity;
import io.renren.modules.generator.export.DtAreaExcelDataCreaterImpl;
import io.renren.modules.generator.service.DtAreaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 区域字典
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-11-26 15:10:04
 */
@RestController
@RequestMapping("generator/dtarea")
public class DtAreaController {
    @Autowired
    private DtAreaService dtAreaService;

    @Autowired
    private ExcelUtils excelUtils;

    @Autowired
    private DtAreaExcelDataCreaterImpl dtAreaExcelDataCreater;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        Query query = new Query(params);

        List<DtAreaEntity> list = dtAreaService.queryPage(query);

        int total =dtAreaService.queryPageTotal(params);

        PageUtils pageUtils = new PageUtils(list, total, query.getLimit(), query.getCurrPage());

        return R.ok().put("page", pageUtils);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
			DtAreaEntity dtArea = dtAreaService.selectById(id);

        return R.ok().put("dtArea", dtArea);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:dtarea:save")
    public R save(@RequestBody DtAreaEntity dtArea){
        return dtAreaService.save(dtArea);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:dtarea:update")
    public R update(@RequestBody DtAreaEntity dtArea){
			return dtAreaService.update(dtArea);

    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:dtarea:delete")
    public R delete(@RequestBody Integer[] ids){
			dtAreaService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    @GetMapping("/queryOmsMnemonicCode")
    public R queryOmsMnemonicCode(@RequestParam("code") String code){
        List<Map<String,String>> maps =dtAreaService.queryOmsMnemonicCode(code);
        return R.ok().put("data",maps);
    }


    /**
     * 由id集合查询地址
     */
    @PostMapping("/queryByIds")
    public R queryByIds(@RequestBody List<Integer> ids){
        List<DtAreaEntity> dtAreaEntities = dtAreaService.selectBatchIds(ids);

        return R.ok().put("data",dtAreaEntities);
    }

    @GetMapping("export")
    @RequiresPermissions("generator:dtarea:export")
    public void exportDate(@RequestParam Map<String, Object> params, HttpServletRequest request,
                           HttpServletResponse response) throws Exception {
        String[] lableNames = {"区域主键","区域名称","区域简称","区域序列","上级主键","助记码","城市代码"};
        String[] columnNames={"id","areaName","areaCode","areaShort","areaSequence","areaParentId","mnemonicCode","omsMnemonicCode"};
        excelUtils.export(response,"数据导出.xlsx",lableNames,columnNames,params,dtAreaExcelDataCreater);

    }



}
